import javax.swing.JFrame;
import  java.awt.*;

public class Ballgame3 extends JFrame {
    //加载游戏图片
    Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");
    Image desk = Toolkit.getDefaultToolkit().getImage("images/desk.png");

    //指定小球的初始位置
    double x =100;
    double y =100;
    double degree = 3.14/3; //3.14=180度
    //画窗口的方法：加载小球与桌面
    public  void paint (Graphics g){
        System.out.println("窗口被画了一次");
        g.drawImage(desk,0,0,null);
        g.drawImage(ball,(int)x,(int)y,null);

        //角度改变小球坐标
        x = x+10*Math.cos(degree);
        y = y+10*Math.sin(degree);

        //边界检测
        if(y>500-40-30||y<40+40){
            degree = -degree;
        }
        if(x<40||x>856-40-30){
            degree = 3.14-degree;
        }
    }
    //窗口加载
    void launchFrame(){
        setSize(856,500);
        setLocation(50,50);
        setVisible(true);

        //重画窗口，每秒画25次
        while(true){
            repaint();  //调用repaint方法，窗口即可重画
            try{
                Thread.sleep(40);  //40ms,1秒=1000毫秒.  大约一秒画25次窗口
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        System.out.println("游戏开始");
        Ballgame3 game = new Ballgame3();
        game.launchFrame();
    }
}
